package com.dark;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * Created by tengxue on 16-6-21.
 */
@ContextConfiguration(locations = {"classpath*:applicationContext.xml"})
public class RedisCollectionDemo extends AbstractJUnit4SpringContextTests {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    private static final String  COLLECTION_List_NAME="collection_list";
    private static final String  COLLECTION_SET_NAME="collection_set";
    private static final String  COLLECTION_ZSET_NAME="collection_zset";

    @Test
    public void testAddElement() {
        List<String> entities=new ArrayList<String>();
        entities.add("a");
        entities.add("a");
        entities.add("b");
        entities.add("c");
        entities.add("d");
        Long result=stringRedisTemplate.opsForList().leftPushAll(COLLECTION_List_NAME, entities);
        System.out.println("the result is :"+result);
    }

    @Test
    public void testGetAllElement() {
        List<String> result=stringRedisTemplate.opsForList().range(COLLECTION_List_NAME, 0, -1);
        System.out.println("the result is :" + result);
    }

    /**
     * 先进先出,可以用作队列
     */
    @Test
    public void testLPushRpop() {
        stringRedisTemplate.opsForList().leftPush(COLLECTION_List_NAME, "l-1");
        testGetAllElement();
        String element=stringRedisTemplate.opsForList().rightPop(COLLECTION_List_NAME);
        System.out.println("the right element is :"+element);
        testGetAllElement();

    }

    /**
     * 保留范围内的元素.
     */
    @Test
    public void testTrim() {
        stringRedisTemplate.opsForList().trim(COLLECTION_List_NAME, 1, 3);
        testGetAllElement();

    }

    /**
     * set集合的实现
     */
    @Test
    public void testAddSetElement() {
        List<String> entities=new ArrayList<String>();
        entities.add("a");
        entities.add("a");
        entities.add("b");
        entities.add("c");
        entities.add("d");
        Long result=stringRedisTemplate.opsForSet().add(COLLECTION_SET_NAME, entities.toArray(new String[]{}));
        System.out.println("the result is :" + result);

    }


    @Test
    public void testGetSetElement() {
        Set<String> result=stringRedisTemplate.opsForSet().members(COLLECTION_SET_NAME);
        System.out.println("the result is :" + result);
    }

    @Test
    public void testRemoveElement() {
        Long result=stringRedisTemplate.opsForSet().remove(COLLECTION_SET_NAME, "b");
        System.out.println("the result is :" + result);
        testGetSetElement();
    }

    /**
     * 集合的交集运算
     */
    @Test
    public void testIntersect() {
        stringRedisTemplate.opsForSet().add("test-a", "1", "2", "3", "4", "5");
        stringRedisTemplate.expire("test-a", 5, TimeUnit.MINUTES);
        stringRedisTemplate.opsForSet().add("test-b", "3", "4", "5", "6", "7");
        stringRedisTemplate.expire("test-a", 5, TimeUnit.MINUTES);
        Set<String> result=stringRedisTemplate.opsForSet().intersect("test-a","test-b");
        System.out.println("the result is :" + result);
    }

    /**
     * 集合的差集运算
     */
    @Test
    public void testDifference() {
        stringRedisTemplate.opsForSet().add("test-a", "1", "2", "3", "4", "5");
        stringRedisTemplate.expire("test-a", 5, TimeUnit.MINUTES);
        stringRedisTemplate.opsForSet().add("test-b", "3", "4", "5", "6", "7");
        stringRedisTemplate.expire("test-a", 5, TimeUnit.MINUTES);
        Set<String> result=stringRedisTemplate.opsForSet().difference("test-a", "test-b");
        System.out.println("the result is :" + result);
    }

    /**
     * 集合的并集运算
     */
    @Test
    public void testUnion() {
        stringRedisTemplate.opsForSet().add("test-a", "1", "2", "3", "4", "5");
        stringRedisTemplate.expire("test-a", 5, TimeUnit.MINUTES);
        stringRedisTemplate.opsForSet().add("test-b", "3", "4", "5", "6", "7");
        stringRedisTemplate.expire("test-a", 5, TimeUnit.MINUTES);
        Set<String> result=stringRedisTemplate.opsForSet().union("test-a", "test-b");
        System.out.println("the result is :" + result);
    }


    @Test
    public void testAddZSetElement() {
        stringRedisTemplate.opsForZSet().add(COLLECTION_ZSET_NAME,"a",0);
        stringRedisTemplate.opsForZSet().add(COLLECTION_ZSET_NAME, "b", 0);
        stringRedisTemplate.opsForZSet().add(COLLECTION_ZSET_NAME,"c",0);
        stringRedisTemplate.opsForZSet().add(COLLECTION_ZSET_NAME, "d", 0);
        stringRedisTemplate.opsForZSet().add(COLLECTION_ZSET_NAME, "e", 0);

    }

    @Test
    public void testGetAllZSetElement() {
       Set<String> result=stringRedisTemplate.opsForZSet().range(COLLECTION_ZSET_NAME,0,-1);
        System.out.println("the result is :" + result);
    }

    @Test
    public void testIncrementScore() {
        stringRedisTemplate.opsForZSet().incrementScore(COLLECTION_ZSET_NAME, "a", 2);
        stringRedisTemplate.opsForZSet().incrementScore(COLLECTION_ZSET_NAME, "c", 1);
        testGetAllZSetElement();
    }

    @Test
    public void testRangeWithScores() {
        Set<ZSetOperations.TypedTuple<String>> typedTuples=stringRedisTemplate.opsForZSet().rangeWithScores(COLLECTION_ZSET_NAME,0,-1);
        for (ZSetOperations.TypedTuple entity:typedTuples){
            System.out.println(entity.getValue()+":"+entity.getScore());
        }
    }

}
